/*
 * Copyright (C) 2011 Mette Bank, Rikke Jensen, Kenneth Brodersen, Thomas Pedersen
 * 
 * This file is part of digiPECS.
 * 
 * digiPECS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * digiPECS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with digiPECS.  If not, see <http://www.gnu.org/licenses/>.
 */

package sw6.digipecs.widget;

import sw6.digipecs.R;
import sw6.digipecs.model.PECSBook;
import sw6.digipecs.model.PECSBook.PECSBookChangeListener;
import sw6.digipecs.widget.dragdrop.DropTarget;
import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;

public class PageBarLayout extends LinearLayout implements PECSBookChangeListener, DropTarget {
	
	private LayoutInflater mInflater;
	private TextView mLastTouched;
	private ViewGroup mBar;
	private PECSBook mBook;
	private final Rect mRect = new Rect();

	public PageBarLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    if(mInflater != null){       
	    	mInflater.inflate(R.layout.pagebarlayout, this);
	    }
	    
	    mBar = (ViewGroup)findViewById(R.id.linear1);
	}
	
	public void setBook(PECSBook book) {
		mBook = book;
		
		// We would like to be informed very time book is changed, if we have to change the number of buttons.
		mBook.addOnChangeListener(this);
		updateButtons();
	}
	
	private void setClicked(View tv) {
		tv.setBackgroundResource(R.drawable.pagebuttonclicked);
		tv.getLayoutParams().height = 60;
		tv.getLayoutParams().width = 60;
	}
	
	private void setUnClicked(View tv) {
		tv.setBackgroundResource(R.drawable.pagebutton);
		tv.getLayoutParams().height = 40;
		tv.getLayoutParams().width = 40;
	}
	
	private void updateButtons() {
		TextView textView;
		
		mBar.removeAllViews();
		
		for (int pos = 0; pos < mBook.getPageCount(); pos++) {
			
			// Create text view and add it to bar
			textView = (TextView) mInflater.inflate(R.layout.pagebutton, mBar, false);
			mBar.addView(textView);
			
			// Set text
			textView.setText("" + (pos + 1));
			
			// Add first as mLastTouched, and set its background
			if(pos == mBook.getPage()){
				setClicked(textView);
				mLastTouched = textView;
			} else {
				setUnClicked(textView);
			}
			
			// Add onClickListener
			final int id = pos;
			textView.setOnClickListener(new OnClickListener(){     	
				public void onClick(View v) {
								
					setUnClicked(mLastTouched);
					mLastTouched.invalidate();
								
					setClicked(v);
					v.invalidate();
					mLastTouched = (TextView)v;
					
					mBook.setPage(id);
				}
			});
			
		}
	}

	@Override
	public void bookChanged() {
		updateButtons();
	}
	
	// TODO This is pretty close to copy of the method in SentenceBoardLayout
	private View findView(int x, int y, Integer index) {
		final ViewGroup parent = mBar;
		final int count = parent.getChildCount();
		final Rect rect = mRect;
		
		for (int i = 0; i < count; i++) {
			final View v = parent.getChildAt(i);
			
			v.getHitRect(rect);
			if (rect.contains(x, y)) {
				if (index != null) index = i;
				return v;
			}
		}
		
		return null;
	}

	@Override
	public boolean drop(View view, int x, int y) {
		// Remember to set view.setVisibility(VISIBLE); if we return true
		return false;
	}

	@Override
	public void onDragIn(int x, int y) {
		final View view = findView(x, y, null);
		if (view != null && view != mLastTouched) view.performClick();
	}

	@Override
	public void onDragOut() {
	}
	
	private Point getCoordinatesForButton(int pagenum) {
		int x = 0;
		int y = 0;
		ViewGroup vg = mBar;
		
		// Find offset from view parents
		while (vg != null) {
			x += vg.getLeft();
			y += vg.getTop();
			
			ViewParent vp = vg.getParent();
			while (vp != null && !(vp instanceof ViewGroup)) vp = vp.getParent();
			vg = (ViewGroup) vp;
		}
		
		// Find offset for view
		final View view = mBar.getChildAt(pagenum);
		x += view.getLeft();
		y += view.getTop();
		
		// Add an half to get center
		x += view.getWidth() / 2;
		y += view.getHeight() / 2;
		
		return new Point(x, y);
	}
	
	// Create an animation ending at a button in this bar
	public Animation createAnimationToPageBar(int page, int fromX, int fromY, int bitmapOffsetX, int bitmapOffsetY, AnimationListener al) {
		final Point p = getCoordinatesForButton(page);
		int toX = p.x - bitmapOffsetX;
		int toY = p.y - bitmapOffsetY;
		
		// Animate the move
		Animation moveAnimation = new TranslateAnimation(fromX, toX, fromY, toY);
		moveAnimation.setDuration(250);
		moveAnimation.setAnimationListener(al);
		
		// Animate the scale
		Animation scaleAnimation = new ScaleAnimation(1, 0, 1, 0, toX + bitmapOffsetX, toY + bitmapOffsetY);
		scaleAnimation.setDuration(250);
		
		// Combine it
		AnimationSet setAnimation = new AnimationSet(false);
		setAnimation.addAnimation(moveAnimation);
		setAnimation.addAnimation(scaleAnimation);
		
		return setAnimation;
	}
	
}

